package com.fbinggun.fattribute.attribute.other;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.AOther;
import com.fbinggun.fattribute.data.EntityData;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.UUID;

public class Health extends AOther {
   public static  UUID uuid=UUID.fromString("42fac09c-8c23-3820-b535-7d71723535be");

    @Override
    public String getKey() {
        return FAttributePlugin.plugin.getConfigKey("Health");
    }


    @Override
    public int getCombat() {
        return 10;
    }

    @Override
    public void up(EntityData entityData) {
        new BukkitRunnable() {
            @Override
            public void run() {
                double health = getValue(entityData);
                AttributeInstance attributeInstance = entityData.getEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH);
                if (attributeInstance.getModifiers() != null) {
                    for (AttributeModifier modifier : attributeInstance.getModifiers()) {
                        if (modifier.getUniqueId()==uuid) {
                            if (modifier.getAmount() == health) {
                                return;
                            }
                        }
                    }
                }
                AttributeModifier modifier = new AttributeModifier(uuid, "FAttributePlugin_Health", health, AttributeModifier.Operation.ADD_NUMBER);
                attributeInstance.removeModifier(modifier);
                if (health == 0) {
                    return;
                }
                attributeInstance.addModifier(modifier);
            }

        }.runTask(FAttributePlugin.plugin);

    }
    public static void remove(EntityData entityData){
        new BukkitRunnable() {
            @Override
            public void run() {
                AttributeInstance attributeInstance = entityData.getEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH);
                if (attributeInstance!=null&&attributeInstance.getModifiers() != null) {
                    for (AttributeModifier modifier : attributeInstance.getModifiers()) {
                        if (modifier.getName().contains("FAttributePlugin_Health")) {
                            attributeInstance.removeModifier(modifier);
                        }
                    }
                }

            }

        }.runTask(FAttributePlugin.plugin);
    }
    @Override
    public String getID() {
        return "Health";
    }
}
